/*---------------------------------------------------------------------------*
 * Copyright (C) 2011-2012 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 * File Name   : drv_bms.h
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This driver is responsible for controlling the Watts-rock
 *               battery PAC FET, reading the voltage/current,
 *               and monitoring the temperature.
 *
 * Last Update : Oct 30, 2012
 *---------------------------------------------------------------------------*/
#ifndef drv_bms_h

   /*------------------------------------------------------------------------*
    * Global Defines
    *------------------------------------------------------------------------*/
   #define drv_bms_h
   #define BMS_DEVICE_ON  (1)
   #define BMS_DEVICE_OFF (0)

   /*------------------------------------------------------------------------*
    * Include Files
    *------------------------------------------------------------------------*/
   #include "avr_compiler.h"
   #include "utl_adc.h"
   #include "utl_timer.h"

   /*------------------------------------------------------------------------*
    * Global Typedefs
    *------------------------------------------------------------------------*/
   typedef enum
   {
      
      BMS_INVALID_ARG = -3, /*Invalid ioctl argument*/
      BMS_INVALID_CMD = -2, /*Invalid ioctl command.*/
      BMS_OUT_OF_HEAP = -1, /*No more memory.*/
      BMS_PASSED      = 0   /*Configuration good.*/

   }t_bmsError; /*Possible error conditions returned by the device's
                  ioctl routine*/

   /*------------------------------------------------------------------------*
    * This data type defines the list of ioctl commands.
    *------------------------------------------------------------------------*/
   typedef enum
   {
      BMS_STORE_CAL = 0,         /*Store voltage and current calibration
                                   factors*/
      BMS_GET_MEAS,              /*Grab a voltage and current measurement*/
      BMS_PACK_CNTRL,            /*Turn on/off the discharge FET*/
      BMS_PWR_CNTRL,             /*Turn on/off the power to the motherboard*/
      BMS_FAN_CNTRL,             /*Turn on/off the fan*/
   }t_bmsCmd;

   /*------------------------------------------------------------------------*
    * This data type defines how data is returned from the device when
    * performing a voltage, current, and temperature measurement.
    *------------------------------------------------------------------------*/
   typedef struct
   {
      /*---------------------------------------------------------------------*
       * The measured battery voltage.
       *---------------------------------------------------------------------*/
      float f_volts;
      /*---------------------------------------------------------------------*
       * The instantaneous measured battery current (in Amps).
       *---------------------------------------------------------------------*/
      float f_current;
      /*---------------------------------------------------------------------*
       * The average battery current (in Amps).
       *---------------------------------------------------------------------*/
      float f_avrCurrent;
      /*---------------------------------------------------------------------*
       * The measured PAC output current (in Amps).
       *---------------------------------------------------------------------*/
      float f_pacCurrent;
      /*---------------------------------------------------------------------*
       * The measured temperature near the PAC switch (in degrees C).
       *---------------------------------------------------------------------*/
      float f_temp;
   }t_bmsMeas;

   /*------------------------------------------------------------------------*
    * This data type defines how the voltage, current, and temperature
    * calibration factors are passed into the device driver.
    *------------------------------------------------------------------------*/
   typedef struct
   {
      /*---------------------------------------------------------------------*
       * The measured bias on the 'volt meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calVltMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the 'current meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calCurMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the PAC output current measurement.
       *---------------------------------------------------------------------*/
      float f_calPacCurMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the 'temperature meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calTempMtrBias;
      /*---------------------------------------------------------------------*
       * The measured resistance of the 'volt meter' voltage divider- in ohms.
       *---------------------------------------------------------------------*/
      float f_calVltMtrRDivide;
      /*---------------------------------------------------------------------*
       * The measured resistance of the 'current meter' R sense resister
       * - in ohms.
       *---------------------------------------------------------------------*/
      float f_calRsense;
      /*---------------------------------------------------------------------*
       * The measured resistance of the PAC current measurement R sense 
       * resister - in ohms.
       *---------------------------------------------------------------------*/
      float f_calPacRsense;
   }t_bmsCal;

   typedef struct
   {
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the battery voltage measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_battVoltAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the battery voltage
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battVoltPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the battery voltage
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battVoltNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the battery current measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_battCurrAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the battery current
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battCurrPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the battery current
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battCurrNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the  temperature
       * measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_tempAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the BMS module
       * temperature measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_tempPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the BMS module
       * temperature measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_tempNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the PAC output current measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_pacCurrAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the Sepic input
       * current measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_pacCurrPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the PAC current 
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_pacCurrNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate PORT for controlling the PAC FET.
       *---------------------------------------------------------------------*/
      uint8_t c_pacFetPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for controlling the PAC FET.
       *---------------------------------------------------------------------*/
      uint8_t c_pacFetPin;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off power to the BMS module.
       *---------------------------------------------------------------------*/
      uint8_t c_shutdownPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off power to the
       * BMS module.
       *---------------------------------------------------------------------*/
      uint8_t c_shutdownPin;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off power to the fan.
       *---------------------------------------------------------------------*/
      uint8_t c_fanPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off power to the
       * fan.
       *---------------------------------------------------------------------*/
      uint8_t c_fanPin;
      /*---------------------------------------------------------------------*
       * This value represents a unique number assigned to this driver and has
       * to be different from all others registered with the kernel.
       *---------------------------------------------------------------------*/
      uint8_t c_majorNum;

   }t_bmsSetup;

   /*------------------------------------------------------------------------*
    * Global Variables
    *------------------------------------------------------------------------*/

   /*------------------------------------------------------------------------*
    * Global Inline functions
    *------------------------------------------------------------------------*/

   /*------------------------------------------------------------------------*
    * Global Function Prototypes
    *------------------------------------------------------------------------*/
   t_error drv_bmsInit( t_bmsSetup t_setup);

   void drv_bmsExit( void);

#endif/*End #ifndef drv_bms_h*/
